package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private FlavorMapper dishFlavorMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断是否有该菜品
        log.info("添加购物车：{}", shoppingCartDTO);
        //获取用户id
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectByDishIdAndUserId(shoppingCart);

        //如何有该菜品则直接修改数量
        if(shoppingCart1 != null){
            BeanUtils.copyProperties(shoppingCart1,shoppingCart);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberByUserIdAndDishId(shoppingCart);
        }else{
            //如何没有该菜品则添加
            //判断是菜品还是套餐
            if(shoppingCartDTO.getDishId() != null){
                //查找并获取相应的菜品
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                //添加菜品到购物车
                BeanUtils.copyProperties(dish,shoppingCart);
                shoppingCart.setAmount(dish.getPrice());
            }else if(shoppingCartDTO.getSetmealId() != null){
                Setmeal setmeal = setmealMapper.selectSetmealBySetmealId(shoppingCartDTO.getSetmealId());
                BeanUtils.copyProperties(setmeal,shoppingCart);
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            /**
             * TODO 这里需要优化,当前时间为成功注入
             */
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        return shoppingCartList;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询购物车菜品数量
        shoppingCart = shoppingCartMapper.selectByDishIdAndUserId(shoppingCart);
        //数量减一
        int number = shoppingCart.getNumber() - 1;
        if(number == 0){
            //删除表数据
            shoppingCartMapper.deleteByDishIdAndSetmealAndUserId(shoppingCart);
        }else{
            //修改表数据-->只数量
            shoppingCart.setNumber(number);
            shoppingCartMapper.sub(shoppingCart);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
